﻿Imports Autodesk.AutoCAD.DatabaseServices
Imports Licenta3.Commanding
Imports Autodesk.AutoCAD.Geometry
Imports Licenta3.Common.Classes
Imports System.Drawing.Printing
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors

Public Class Anexa11ViewModel
    Inherits AnexaViewModel(Of Anexa11Class)

    Public Sub New()
        Anexa.Autorizat = Setari.currentAutorizat
        Anexa.OCPI = Anexa.Autorizat.DefaultOCPI
        Anexa.BCPI = Anexa.Autorizat.DefaultBCPI

        _SelectParcelaGeometryCommand = New DelegateCommand(Of Object)(AddressOf SelectParcelaGeometry, AddressOf CanSelectParcelaGeometry)
        _SelectConstructieGeometryCommand = New DelegateCommand(Of Object)(AddressOf SelectConstructieGeometry, AddressOf CanSelectConstructieGeometry)
        _SelectImobilGeometryCommand = New DelegateCommand(Of Object)(AddressOf SelectImobilGeometry, AddressOf CanSelectImobilGeometry)
        _GenereazaPADCommand = New DelegateCommand(Of Object)(AddressOf GenereazaPAD, AddressOf CanGenereazaPAD)
    End Sub
    Public Property SelectParcelaGeometryCommand As ICommand
    Private _DataPAD As Date = Today
    Public Property DataPAD As Date
        Get
            Return _DataPAD
        End Get
        Set(value As Date)
            _DataPAD = value
            OnPropertyChanged("DataPAD")
        End Set
    End Property


    Public Sub SelectParcelaGeometry(parcela As Parcela)
        parcela.Polyline.SelectGeometry()
        'MsgBox(im.Geometry.NumberOfVertices.ToString)
    End Sub
    Public Function CanSelectParcelaGeometry()
        Return True
    End Function
    Public Property SelectConstructieGeometryCommand As ICommand
    Public Sub SelectConstructieGeometry(constr As Constructie)
        constr.Polyline.SelectGeometry()
        'MsgBox(im.Geometry.NumberOfVertices.ToString)
    End Sub
    Public Function CanSelectConstructieGeometry()
        Return True
    End Function
    Public Property SelectImobilGeometryCommand As ICommand
    Public Sub SelectImobilGeometry()
        Anexa.Imobil.Polyline.SelectGeometry()
        Anexa.Imobil.UpdateVecini()

    End Sub
    Public Function CanSelectImobilGeometry()
        Return True
    End Function
    Public Property GenereazaPADCommand As ICommand
    Public Property CategoriiFolosinta As New CategorieFolosinta
    Private Sub pesteZece()
        MessageBox.Show("Suprafața din teren e cu mai mult de 10% față de cea din act")
    End Sub
    Public Sub GenereazaPAD()
        Dim t As New System.Threading.Thread(AddressOf pesteZece)
        If (Anexa.Imobil.Polyline.Geometry.Area - Anexa.Imobil.SuprafataDinAct) / Anexa.Imobil.SuprafataDinAct >= 0.1 Then
            t.Start()
        End If
        Try
            Dim maxHeight, maxWidth As Integer
            Dim jos, sus As Integer
            Dim topMargin As Integer = 10
            Dim bottomMargin As Integer = 10
            Dim leftMargin As Integer = 20
            Dim rightMargin As Integer = 10
            Dim tblMargin As Integer = 10

            Dim rowHeight As Decimal = 3.5
            Dim txtRowHeight As Decimal = 2.5
            Dim A4p, A4l, A3p, A3l, paper, chenar As New Rectangle
            A4p.Height = 297
            A4p.Width = 210
            A4l.Height = 210
            A4l.Width = 297
            A3p.Height = 420
            A3p.Width = 297
            A3l.Height = 297
            A3l.Width = 420
            paper = A4p
            Dim bl, br, tr, tl, o As New Point2d
            Dim fitScale As Decimal
            Dim sc As Scari = Scari.NA
            Dim scA As Decimal
            Dim paperFound As Boolean = False
            sus = rowHeight * 8
            jos = tblMargin + rowHeight * (20 + Anexa.Imobil.Parcele.Count + Anexa.Imobil.Constructii.Count + Anexa.Imobil.Polyline.Geometry.NumberOfVertices)
            For i = 1 To 2

                maxHeight = paper.Height - topMargin - bottomMargin - sus - jos
                maxWidth = paper.Width - leftMargin - rightMargin - 2 * rowHeight ''rowHeight sunt pentru coordonate
                'MsgBox(maxHeight & " " & maxWidth)
                bl = New Point2d(Math.Floor(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MinPoint.X / 5) * 5, Math.Floor(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MinPoint.Y / 5) * 5)
                tr = New Point2d(Math.Ceiling(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MaxPoint.X / 5) * 5, Math.Ceiling(Anexa.Imobil.Polyline.Geometry.GeometricExtents.MaxPoint.Y / 5) * 5)
                br = New Point2d(tr.X, bl.Y)
                tl = New Point2d(bl.X, tr.Y)
                fitScale = Math.Max((tr.Y - bl.Y) * 1000 / maxHeight, (tr.X - bl.X) * 1000 / maxWidth)
                For Each value As Scari In [Enum].GetValues(GetType(Scari))
                    If value > fitScale Then
                        sc = value
                        Exit For
                    End If
                Next
                If sc <> Scari.NA Then
                    Exit For
                Else
                    paper = A3p
                End If
            Next
            'MsgBox(sc.ToString)
            If sc <> Scari.NA Then
                Anexa.Scara = sc
            Else
                MsgBox("Nu incape nici pe A3 - 1 la 5000!")
                Exit Sub
            End If
            scA = CDec(sc / 1000)
            chenar.Width = (paper.Width - leftMargin - rightMargin)
            chenar.Height = (paper.Height - topMargin - topMargin)
            o = New Point2d(bl.X - ((paper.Width - leftMargin - rightMargin) * scA - (br.X - bl.X)) / 2, (bl.Y + tr.Y) / 2 - (maxHeight / 2 + jos) * scA)

            '' Get the current document and database
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database
            'Try
            '    Dim nordDb As New Database(False, True)
            '    nordDb.ReadDwgFile(assemblyPath + "\blocks\nord.dwg", FileOpenMode.OpenForReadAndAllShare, True, "")
            '    Dim nid As ObjectId
            '    nid = acCurDb.Insert("nord", nordDb, True)
            '    Dim nordId As New ObjectIdCollection
            '    Using nordTrans As Transaction = nordDb.TransactionManager.StartTransaction
            '        Dim nordBlkTbl As BlockTable = CType(nordTrans.GetObject(nordDb.BlockTableId, OpenMode.ForRead, False), BlockTable)
            '        nordId.Add(nordBlkTbl("nord"))
            '        nordTrans.Commit()
            '    End Using
            '    nordDb.WblockCloneObjects(nordId, acCurDb.BlockTableId, New IdMapping, DuplicateRecordCloning.Ignore, False)
            '    nordDb.Dispose()

            'Catch ex As Exception
            '    MsgBox("Eroare la importarea nordului")
            'End Try
            '' Start a transaction
            Using dl As DocumentLock = acDoc.LockDocument(DocumentLockMode.ProtectedAutoWrite, Nothing, Nothing, True)
                Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()



                    '' Open the Block table for read
                    Dim acBlkTbl As BlockTable
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                    '' Open the Block table record Model space for write

                    Dim acBlkTblRec As BlockTableRecord
                    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                    Dim acLyrTbl As LayerTable
                    acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
                    Dim oldLayer As LayerTableRecord
                    oldLayer = CType(acTrans.GetObject(acCurDb.Clayer, OpenMode.ForRead), LayerTableRecord)
                    Dim AIMLayerName As String = "AIM"
                    If acLyrTbl.Has(AIMLayerName) = False Then
                        Dim acLyrTblRec As LayerTableRecord = New LayerTableRecord()
                        '' Assign the layer the ACI color 1 and a name
                        acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
                        acLyrTblRec.Name = AIMLayerName
                        '' Upgrade the Layer table for write
                        acLyrTbl.UpgradeOpen()
                        '' Append the new layer to the Layer table and the transaction
                        acLyrTbl.Add(acLyrTblRec)
                        acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
                        acCurDb.Clayer = acLyrTbl(AIMLayerName)
                    Else
                        acCurDb.Clayer = acLyrTbl(AIMLayerName)
                    End If
                    ''inserare nord
                    Try
                        Dim nordDb As New Database(False, True)
                        nordDb.ReadDwgFile(assemblyPath + "\blocks\nord.dwg", FileOpenMode.OpenForReadAndAllShare, True, "")
                        Dim nid As ObjectId = acCurDb.Insert("nord", nordDb, True)
                        Dim nordRef As New BlockReference(New Point3d(tr.X + 8, tr.Y, 0), nid)
                        nordRef.ScaleFactors = New Scale3d(scA, scA, scA)
                        acBlkTblRec.AppendEntity(nordRef)
                        acTrans.AddNewlyCreatedDBObject(nordRef, True)
                        nordDb.Dispose()
                    Catch ex As Exception
                        MsgBox(ex.Message + ex.Source)

                    End Try

                    Dim paperRect As New Polyline(4)
                    paperRect.AddVertexAt(0, New Point2d(o.X - scA * leftMargin, o.Y - scA * bottomMargin), Nothing, Nothing, Nothing)
                    paperRect.AddVertexAt(0, New Point2d(o.X + scA * (paper.Width - leftMargin), o.Y - scA * bottomMargin), Nothing, Nothing, Nothing)
                    paperRect.AddVertexAt(0, New Point2d(o.X + scA * (paper.Width - leftMargin), o.Y + scA * (paper.Height - topMargin)), Nothing, Nothing, Nothing)
                    paperRect.AddVertexAt(0, New Point2d(o.X - scA * leftMargin, o.Y + scA * (paper.Height - topMargin)), Nothing, Nothing, Nothing)
                    paperRect.Closed = True
                    acBlkTblRec.AppendEntity(paperRect)
                    acTrans.AddNewlyCreatedDBObject(paperRect, True)
                    Dim col1 As New Point3dCollection
                    For i = 0 To paperRect.NumberOfVertices - 1
                        col1.Add(paperRect.GetPoint3dAt(i))
                    Next
                    '' ''Dim reg As New Region
                    '' ''Dim ceva As New DBObjectCollection
                    '' ''paperRect.Explode(ceva)
                    '' ''reg = Region.CreateFromCurves(ceva)(0)
                    '' ''Dim strPoints As New Point3dCollection
                    '' ''reg.GetStretchPoints(strPoints)
                    '' ''If col1.Count = strPoints.Count Then
                    '' ''    For i = 0 To col1.Count - 1
                    '' ''        MsgBox(col1(i).X.ToString & "=" & strPoints(i).X.ToString)
                    '' ''        MsgBox(col1(i).Y.ToString & "=" & strPoints(i).Y.ToString)
                    '' ''    Next
                    '' ''Else
                    '' ''    MsgBox("nici macar n-au acelasi numar de puncte")
                    '' ''End If
                    Dim chenarRect As New Polyline(4)
                    chenarRect.AddVertexAt(0, o, Nothing, Nothing, Nothing)
                    chenarRect.AddVertexAt(0, New Point2d(o.X + scA * chenar.Width, o.Y), Nothing, Nothing, Nothing)
                    chenarRect.AddVertexAt(0, New Point2d(o.X + scA * chenar.Width, o.Y + scA * chenar.Height), Nothing, Nothing, Nothing)
                    chenarRect.AddVertexAt(0, New Point2d(o.X, o.Y + scA * chenar.Height), Nothing, Nothing, Nothing)
                    chenarRect.Closed = True
                    acBlkTblRec.AppendEntity(chenarRect)
                    acTrans.AddNewlyCreatedDBObject(chenarRect, True)

                    Dim title As New DBText
                    title.SetDatabaseDefaults()
                    title.HorizontalMode = TextHorizontalMode.TextMid
                    title.VerticalMode = TextVerticalMode.TextBottom
                    title.AlignmentPoint = New Point3d(o.X + scA * chenar.Width / 2, o.Y + scA * (chenar.Height - 1.5 * rowHeight), 0)
                    title.TextString = "Plan de amplasament și delimitare"
                    title.Height = 3.5 * scA
                    title.AdjustAlignment(acCurDb)
                    acBlkTblRec.AppendEntity(title)
                    acTrans.AddNewlyCreatedDBObject(title, True)

                    Dim scara As New DBText
                    scara.SetDatabaseDefaults()
                    scara.HorizontalMode = TextHorizontalMode.TextMid
                    scara.VerticalMode = TextVerticalMode.TextBottom
                    scara.AlignmentPoint = New Point3d(title.AlignmentPoint.X, title.AlignmentPoint.Y - scA * 1.5 * rowHeight, 0)
                    scara.TextString = "Scara 1:" & CInt(sc).ToString
                    scara.Height = 3 * scA
                    scara.AdjustAlignment(acCurDb)
                    acBlkTblRec.AppendEntity(scara)
                    acTrans.AddNewlyCreatedDBObject(scara, True)

                    Dim tabel1 As New Table
                    tabel1.SetDatabaseDefaults()
                    tabel1.Position = New Point3d(o.X + scA * tblMargin, o.Y + scA * (chenar.Height - 4 * rowHeight), 0)
                    tabel1.SetSize(2, 3)
                    tabel1.SetRowHeight(rowHeight * scA)
                    tabel1.Cells.TextHeight = txtRowHeight * scA
                    tabel1.Columns(0).Width = 30 * scA
                    tabel1.Columns(1).Width = 50 * scA
                    tabel1.Columns(2).Width = (chenar.Width - 2 * tblMargin) * scA - tabel1.Columns(0).Width - tabel1.Columns(1).Width
                    tabel1.Cells(0, 0).TextString = "Nr. Cadastral"
                    tabel1.Cells(0, 1).TextString = "Suprafata Măsurată"
                    tabel1.Cells(0, 2).TextString = "Adresa Imobilului"
                    tabel1.Cells(1, 1).TextString = Math.Round(Anexa.Imobil.Polyline.Geometry.Area, 1).ToString & " mp"
                    If Anexa.Imobil.Adresa.adresaIntreaga IsNot Nothing Then tabel1.Cells(1, 2).TextString = Anexa.Imobil.Adresa.adresaIntreaga.ToString
                    acBlkTblRec.AppendEntity(tabel1)
                    acTrans.AddNewlyCreatedDBObject(tabel1, True)

                    Dim tabel2 As New Table
                    With tabel2
                        .SetDatabaseDefaults()
                        .Position = New Point3d(tabel1.Position.X, tabel1.Position.Y - tabel1.MinimumTableHeight, 0)
                        .SetSize(1, 4)
                        .SetRowHeight(rowHeight * scA)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Columns(0).Width = 50 * scA
                        .Columns(1).Width = 30 * scA
                        .Columns(2).Width = 30 * scA
                        .Columns(3).Width = (chenar.Width - 2 * tblMargin) * scA - .Columns(0).Width - .Columns(1).Width - .Columns(2).Width
                        .Cells(0, 0).TextString = "Carte Funciară Nr."
                        .Cells(0, 1).TextString = ""
                        .Cells(0, 2).TextString = "UAT"
                        .Cells(0, 3).TextString = Anexa.Imobil.UAT.Denumire
                    End With
                    acBlkTblRec.AppendEntity(tabel2)
                    acTrans.AddNewlyCreatedDBObject(tabel2, True)

                    Dim coordRect As New Polyline(4)
                    With coordRect
                        .AddVertexAt(0, bl, Nothing, Nothing, Nothing)
                        .AddVertexAt(1, br, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, tr, Nothing, Nothing, Nothing)
                        .AddVertexAt(3, tl, Nothing, Nothing, Nothing)
                        .Closed = True
                    End With
                    acBlkTblRec.AppendEntity(coordRect)
                    acTrans.AddNewlyCreatedDBObject(coordRect, True)

                    Dim lineBL As New Polyline(3)
                    With lineBL
                        .AddVertexAt(0, New Point2d(bl.X - 4 * scA, bl.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, bl, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(bl.X, bl.Y - 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineBL)
                    acTrans.AddNewlyCreatedDBObject(lineBL, True)
                    Dim NBL As New DBText
                    With NBL
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(bl.X, bl.Y, 0)
                        .TextString = "N=" & CInt(bl.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NBL)
                    acTrans.AddNewlyCreatedDBObject(NBL, True)
                    Dim EBL As New DBText
                    With EBL
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(bl.X, bl.Y, 0)
                        .TextString = "E=" & CInt(bl.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(EBL)
                    acTrans.AddNewlyCreatedDBObject(EBL, True)

                    Dim lineBR As New Polyline(3)
                    With lineBR
                        .AddVertexAt(0, New Point2d(br.X + 4 * scA, br.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, br, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(br.X, br.Y - 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineBR)
                    acTrans.AddNewlyCreatedDBObject(lineBR, True)
                    Dim NBR As New DBText
                    With NBR
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(br.X, br.Y, 0)
                        .TextString = "N=" & CInt(br.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NBR)
                    acTrans.AddNewlyCreatedDBObject(NBR, True)
                    Dim EBR As New DBText
                    With EBR
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(br.X, br.Y, 0)
                        .TextString = "E=" & CInt(br.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(EBR)
                    acTrans.AddNewlyCreatedDBObject(EBR, True)

                    Dim lineTR As New Polyline(3)
                    With lineTR
                        .AddVertexAt(0, New Point2d(tr.X + 4 * scA, tr.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, tr, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(tr.X, tr.Y + 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineTR)
                    acTrans.AddNewlyCreatedDBObject(lineTR, True)
                    Dim NTR As New DBText
                    With NTR
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(tr.X, tr.Y, 0)
                        .TextString = "N=" & CInt(tr.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NTR)
                    acTrans.AddNewlyCreatedDBObject(NTR, True)
                    Dim ETR As New DBText
                    With ETR
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextTop
                        .AlignmentPoint = New Point3d(tr.X, tr.Y, 0)
                        .TextString = "E=" & CInt(tr.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(ETR)
                    acTrans.AddNewlyCreatedDBObject(ETR, True)

                    Dim lineTL As New Polyline(3)
                    With lineTL
                        .AddVertexAt(0, New Point2d(tl.X - 4 * scA, tl.Y), Nothing, Nothing, Nothing)
                        .AddVertexAt(1, tl, Nothing, Nothing, Nothing)
                        .AddVertexAt(2, New Point2d(tl.X, tl.Y + 4 * scA), Nothing, Nothing, Nothing)
                    End With
                    acBlkTblRec.AppendEntity(lineTL)
                    acTrans.AddNewlyCreatedDBObject(lineTL, True)
                    Dim NTL As New DBText
                    With NTL
                        .SetDatabaseDefaults()
                        .HorizontalMode = TextHorizontalMode.TextLeft
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(tl.X, tl.Y, 0)
                        .TextString = "N=" & CInt(tl.Y).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(NTL)
                    acTrans.AddNewlyCreatedDBObject(NTL, True)
                    Dim ETL As New DBText
                    With ETL
                        .SetDatabaseDefaults()
                        .Rotation = Math.PI / 2
                        .HorizontalMode = TextHorizontalMode.TextRight
                        .VerticalMode = TextVerticalMode.TextBottom
                        .AlignmentPoint = New Point3d(tl.X, tl.Y, 0)
                        .TextString = "E=" & CInt(tl.X).ToString
                        .Height = 2 * scA
                        .AdjustAlignment(acCurDb)
                    End With
                    acBlkTblRec.AppendEntity(ETL)
                    acTrans.AddNewlyCreatedDBObject(ETL, True)

                    For i = 1 To Anexa.Imobil.Constructii.Count
                        Dim txtC As New DBText
                        'Dim centroid As New Point3d((Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MaxPoint.X + Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MinPoint.X) / 2, _
                        '        (Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MaxPoint.Y + Anexa.Imobil.Constructii(i - 1).Polyline.Geometry.GeometricExtents.MinPoint.Y) / 2, _
                        '        0)
                        With txtC
                            .SetDatabaseDefaults()
                            .HorizontalMode = TextHorizontalMode.TextCenter
                            .VerticalMode = TextVerticalMode.TextVerticalMid
                            .AlignmentPoint = centroid(Anexa.Imobil.Constructii(i - 1).Polyline.Geometry)
                            .TextString = "C" & CInt(i).ToString
                            .Height = 2 * scA
                            .AdjustAlignment(acCurDb)
                        End With
                        acBlkTblRec.AppendEntity(txtC)
                        acTrans.AddNewlyCreatedDBObject(txtC, True)
                    Next
                    numberPolyline(Anexa.Imobil.Polyline.Geometry, scA, acCurDb, acTrans, acBlkTblRec)
                    dimPolyline(Anexa.Imobil.Polyline.Geometry, scA, acCurDb, acTrans, acBlkTblRec)
                    veciniPolyline(Anexa.Imobil.Polyline.Geometry, scA, Anexa.Imobil.Vecini, acCurDb, acTrans, acBlkTblRec)

                    Dim tabelA As New Table
                    With tabelA
                        .SetDatabaseDefaults()
                        .Position = New Point3d(tabel1.Position.X, o.Y + (jos - 2 * rowHeight) * scA, 0)
                        .SetSize(3 + Anexa.Imobil.Parcele.Count, 5)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Rows(0).IsMergeAllEnabled = True
                        .SetRowHeight(rowHeight * scA)
                        .Cells(0, 0).TextString = "A. Date referitoare la teren"
                        .Columns(0).Width = 30 * scA
                        .Columns(1).Width = 30 * scA
                        .Columns(2).Width = 30 * scA
                        .Columns(3).Width = 30 * scA
                        .Columns(4).Width = (chenar.Width - 2 * tblMargin) * scA - .Columns(0).Width - .Columns(1).Width - .Columns(2).Width - .Columns(3).Width
                        .Cells(1, 0).TextString = "Nr. Parcela"
                        .Cells(1, 1).TextString = "Categorie de folosinta"
                        .Cells(1, 2).TextString = "Suprafata"
                        .Cells(1, 3).TextString = "Valoare de impozitare"
                        .Cells(1, 4).TextString = "Mențiuni"
                        '.Rows(1).Height = 2 * rowHeight
                        Dim totalSuprafata, totalValoareImpozitare As Decimal
                        totalSuprafata = 0
                        totalValoareImpozitare = 0
                        Try
                            For i = 2 To .Rows.Count - 2
                                .Cells(i, 0).TextString = (i - 1).ToString
                                .Cells(i, 1).TextString = Anexa.Imobil.Parcele(i - 2).CategorieFolosinta
                                .Cells(i, 2).TextString = Math.Round(Anexa.Imobil.Parcele(i - 2).Polyline.Geometry.Area, 0).ToString
                                totalSuprafata += Anexa.Imobil.Parcele(i - 2).Polyline.Geometry.Area
                                .Cells(i, 3).TextString = Anexa.Imobil.Parcele(i - 2).ValoareImpozitare.ToString
                                totalValoareImpozitare += Anexa.Imobil.Parcele(i - 2).ValoareImpozitare
                                .Cells(i, 4).TextString = Anexa.Imobil.Parcele(i - 2).Imprejmuire
                            Next
                            .Cells(.Rows.Count - 1, 0).TextString = "Total"
                            .Cells(.Rows.Count - 1, 1).TextString = ""
                            .Cells(.Rows.Count - 1, 2).TextString = Math.Round(totalSuprafata, 0).ToString
                            .Cells(.Rows.Count - 1, 3).TextString = totalValoareImpozitare.ToString
                            .Cells(.Rows.Count - 1, 4).TextString = ""
                        Catch ex As Exception
                            MsgBox("TabelA: " + ex.Message + ex.StackTrace)
                        End Try
                    End With
                    acBlkTblRec.AppendEntity(tabelA)
                    acTrans.AddNewlyCreatedDBObject(tabelA, True)
                    Dim tabelB As New Table
                    With tabelB
                        .SetDatabaseDefaults()
                        .Position = New Point3d(tabelA.Position.X, tabelA.Position.Y - tabelA.MinimumTableHeight, 0)
                        .SetSize(3 + Anexa.Imobil.Constructii.Count, 4)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Rows(0).IsMergeAllEnabled = True
                        .SetRowHeight(rowHeight * scA)
                        .Cells(0, 0).TextString = "B. Date referitoare la Construcții"
                        .Columns(0).Width = 30 * scA
                        .Columns(1).Width = 45 * scA
                        .Columns(2).Width = 45 * scA
                        .Columns(3).Width = (chenar.Width - 2 * tblMargin) * scA - .Columns(0).Width - .Columns(1).Width - .Columns(2).Width
                        .Cells(1, 0).TextString = "Cod Constr."
                        .Cells(1, 1).TextString = "Suprafața construită la sol (mp)"
                        .Cells(1, 2).TextString = "Valoare de impozitare"
                        .Cells(1, 3).TextString = "Mențiuni"
                        '.Rows(1).Height = 2 * rowHeight
                        Dim totalSuprafata, totalValoareImpozitare As Decimal
                        totalSuprafata = 0
                        totalValoareImpozitare = 0
                        Try
                            For i = 2 To .Rows.Count - 2
                                .Cells(i, 0).TextString = "C" & (i - 1).ToString
                                .Cells(i, 1).TextString = Math.Round(Anexa.Imobil.Constructii(i - 2).Polyline.Geometry.Area, 0).ToString
                                totalSuprafata += Anexa.Imobil.Constructii(i - 2).Polyline.Geometry.Area
                                .Cells(i, 2).TextString = Anexa.Imobil.Constructii(i - 2).ValoareImpozitare.ToString
                                totalValoareImpozitare += Anexa.Imobil.Constructii(i - 2).ValoareImpozitare
                                .Cells(i, 3).TextString = Anexa.Imobil.Constructii(i - 2).SuprafataDesfasurata
                            Next
                            .Cells(.Rows.Count - 1, 0).TextString = "Total"
                            .Cells(.Rows.Count - 1, 1).TextString = Math.Round(totalSuprafata, 0).ToString
                            .Cells(.Rows.Count - 1, 2).TextString = totalValoareImpozitare.ToString
                            .Cells(.Rows.Count - 1, 3).TextString = ""
                        Catch ex As Exception
                            MsgBox("TabelB: " + ex.Message + ex.StackTrace)
                        End Try
                    End With
                    acBlkTblRec.AppendEntity(tabelB)
                    acTrans.AddNewlyCreatedDBObject(tabelB, True)

                    ''inventar de coordonate
                    Dim tabelInventarCoordonate As New Table
                    With tabelInventarCoordonate
                        .SetDatabaseDefaults()
                        .Position = New Point3d(tabelB.Position.X, tabelB.Position.Y - tabelB.MinimumTableHeight, 0)
                        .SetSize(5 + Anexa.Imobil.Polyline.Geometry.NumberOfVertices, 3)
                        .SetRowHeight(rowHeight * scA)
                        .Cells.TextHeight = txtRowHeight * scA
                        .Rows(0).IsMergeAllEnabled = True
                        .Cells(0, 0).TextString = "INVENTAR DE COORDONATE"
                        Dim rngSistem As CellRange
                        rngSistem = CellRange.Create(tabelInventarCoordonate, 1, 0, 1, 2)
                        .MergeCells(rngSistem)
                        .Rows(1).Height = 3 * rowHeight * scA
                        .Cells(1, 0).TextString = "Sistem de proiecție: Stereografic 1970"
                        .Cells(1, 0).Alignment = CellAlignment.MiddleCenter

                        .Columns(0).Width = 20 * scA
                        .Columns(1).Width = 30 * scA
                        .Columns(2).Width = 30 * scA

                        .Cells(2, 0).TextString = "Pct."
                        .Cells(2, 1).TextString = "E(m)"
                        .Cells(2, 2).TextString = "N(m)"

                        Try
                            For i = 3 To .Rows.Count - 3
                                .Cells(i, 0).TextString = (i - 2).ToString
                                .Cells(i, 1).TextString = Math.Round(Anexa.Imobil.Polyline.Geometry.GetPoint2dAt(i - 3).X, 2).ToString
                                .Cells(i, 2).TextString = Math.Round(Anexa.Imobil.Polyline.Geometry.GetPoint2dAt(i - 3).Y, 2).ToString
                            Next
                            Dim rngSuprTotala As CellRange
                            rngSuprTotala = CellRange.Create(tabelInventarCoordonate, .Rows.Count - 2, 0, .Rows.Count - 2, 2)
                            .MergeCells(rngSuprTotala)
                            .Rows(.Rows.Count - 2).Height = 2 * rowHeight * scA
                            .Cells(.Rows.Count - 2, 0).Alignment = CellAlignment.MiddleCenter
                            .Cells(.Rows.Count - 2, 0).TextString = "Suprafața totală măsurată = " & Math.Round(Anexa.Imobil.Polyline.Geometry.Area, 0).ToString & "m"

                            Dim rngSuprAct As CellRange
                            rngSuprAct = CellRange.Create(tabelInventarCoordonate, .Rows.Count - 1, 0, .Rows.Count - 1, 2)
                            .MergeCells(rngSuprAct)
                            .Rows(.Rows.Count - 1).Height = 2 * rowHeight * scA
                            .Cells(.Rows.Count - 1, 0).Alignment = CellAlignment.MiddleCenter
                            .Cells(.Rows.Count - 1, 0).TextString = "Suprafața din act = " & Anexa.Imobil.SuprafataDinAct & " m"
                        Catch ex As Exception
                            MsgBox("Inventar: " + ex.Message + ex.StackTrace)
                        End Try
                    End With
                    acBlkTblRec.AppendEntity(tabelInventarCoordonate)
                    acTrans.AddNewlyCreatedDBObject(tabelInventarCoordonate, True)

                    Dim tabelStampile As New Table
                    With tabelStampile
                        .SetDatabaseDefaults()
                        .Position = New Point3d(tabelInventarCoordonate.Position.X + tabelInventarCoordonate.Width, tabelInventarCoordonate.Position.Y, 0)
                        .SetSize(3, 1)
                        '.Height = tabelInventarCoordonate.Height
                        .SetRowHeight((tabelInventarCoordonate.Height - rowHeight * scA) / 2)
                        '.Height = tabelInventarCoordonate.Height
                        .SetColumnWidth((chenar.Width - 2 * tblMargin) * scA - tabelInventarCoordonate.Width)
                        .Cells.TextHeight = txtRowHeight * scA
                        Dim enters As String = Nothing
                        For i = 1 To CInt(Math.Floor(.Rows(0).Height / rowHeight)) - 2
                            enters += "\U+000d\U+000a"
                        Next
                        Try
                            .Cells(0, 0).TextString = "Executant"
                            '.Rows(0).Height = .MinimumTableHeight / 2 - rowHeight * scA
                            .Cells(0, 0).Alignment = CellAlignment.TopCenter
                            .Cells(1, 0).TextString = DataPAD.ToShortDateString
                            .Cells(1, 0).Alignment = CellAlignment.MiddleCenter
                            .Rows(1).Height = rowHeight * scA
                            .Cells(2, 0).Alignment = CellAlignment.TopCenter
                            .Cells(2, 0).TextString = "Se confirmă suprafața din măsurători și \U+000d\U+000a Introducerea imobilului în baza de date"
                            '.Rows(2).Height = .Height / 2
                        Catch ex As Exception
                            MsgBox("Tabel Stampile" + ex.Message + ex.StackTrace)
                        End Try
                    End With
                    acBlkTblRec.AppendEntity(tabelStampile)
                    acTrans.AddNewlyCreatedDBObject(tabelStampile, True)

                    'Dim tabelA As New Table
                    'With tabelA
                    '    .SetDatabaseDefaults()
                    '    .Position = New Point3d(tabel1.Position.X, o.Y + jos * scA, 0)
                    '    .SetSize(3 + Anexa.Imobil.Parcele.Count, 5)
                    '    .Cells.TextHeight = txtRowHeight * scA
                    '    .Rows(0).IsMergeAllEnabled = True
                    '    .SetRowHeight(rowHeight * scA)
                    '    .Cells(0, 0).TextString = "A. Date referitoare la teren"
                    '    .Columns(0).Width = 30 * scA
                    '    .Columns(1).Width = 30 * scA
                    '    .Columns(2).Width = 30 * scA
                    '    .Columns(3).Width = 30 * scA
                    '    .Columns(4).Width = (chenar.Width - 2 * tblMargin) * scA - .Columns(0).Width - .Columns(1).Width - .Columns(2).Width - .Columns(3).Width
                    '    .Cells(1, 0).TextString = "Nr. Parcela"
                    '    .Cells(1, 1).TextString = "Categorie de folosinta"
                    '    .Cells(1, 2).TextString = "Suprafata"
                    '    .Cells(1, 3).TextString = "Valoare de impozitare"
                    '    .Cells(1, 4).TextString = "Mentiuni"
                    '    Dim totalSuprafata, totalValoareImpozitare As Decimal
                    '    totalSuprafata = 0
                    '    totalValoareImpozitare = 0
                    '    For i = 2 To .Rows.Count - 2
                    '        .Cells(1, 0).TextString = (i - 1).ToString
                    '        .Cells(1, 1).TextString = Anexa.Imobil.Parcele(i - 2).CategorieFolosinta
                    '        .Cells(1, 2).TextString = Math.Round(Anexa.Imobil.Parcele(i - 2).Polyline.Geometry.Area, 0).ToString
                    '        totalSuprafata += Anexa.Imobil.Parcele(i - 2).Polyline.Geometry.Area
                    '        .Cells(1, 3).TextString = Anexa.Imobil.Parcele(i - 2).ValoareImpozitare.ToString
                    '        totalValoareImpozitare += Anexa.Imobil.Parcele(i - 2).ValoareImpozitare
                    '        .Cells(1, 4).TextString = Anexa.Imobil.Parcele(i - 2).Imprejmuire
                    '    Next
                    '    .Cells(.Rows.Count - 1, 0).TextString = "Total"
                    '    .Cells(.Rows.Count - 1, 1).TextString = ""
                    '    .Cells(.Rows.Count - 1, 2).TextString = Math.Round(totalSuprafata, 0).ToString
                    '    .Cells(.Rows.Count - 1, 3).TextString = totalValoareImpozitare.ToString
                    '    .Cells(.Rows.Count - 1, 4).TextString = ""
                    'End With




                    '' Create a polyline with two segments (3 points)
                    'Dim acPoly As Polyline = New Polyline()
                    'acPoly.SetDatabaseDefaults()
                    'acPoly.AddVertexAt(0, New Point2d(2, 4), 0, 0, 0)
                    'acPoly.AddVertexAt(1, New Point2d(4, 2), 0, 0, 0)
                    'acPoly.AddVertexAt(2, New Point2d(6, 4), 0, 0, 0)
                    '' Add the new object to the block table record and the transaction
                    'acBlkTblRec.AppendEntity(acPoly)
                    'acTrans.AddNewlyCreatedDBObject(acPoly, True)
                    '' Save the new object to the database
                    acCurDb.Clayer = acLyrTbl(oldLayer.Name)
                    acTrans.Commit()
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.Data.ToString)
        End Try





    End Sub
    Public Function CanGenereazaPAD()
        Return True
    End Function
End Class
